package beta;

import javax.microedition.lcdui.Graphics;


public class LabelItem extends Item
{
    private String[] tab;
    private String text;
    public LabelItem(String t)
    {
        super(false);
        parseText(t);
    }
    
    public LabelItem(String t, int a)
    {
        this(t);
        setAlignment(a);
    }
    
    public void setText(String t)
    {
        parseText(t);
    }
    
    public String getText()
    {
        return text;
    }
    
    private void parseText(String t)
    {
        StringBuffer b = new StringBuffer();
        text = t;
        int lines = 1;
        char c;
        for(int i = 0; i < t.length(); ++i)
            if(t.charAt(i) == '\n') lines ++;
        tab = new String[lines];
        lines = 0;
        for(int i = 0; i < t.length(); ++i)
        {
            if((c = t.charAt(i)) == '\n')
            {
                tab[lines++] = b.toString();
                b.setLength(0);
            }
            else
                b.append(c);
        }
        if(b.length() > 0)
            tab[lines] = b.toString();
    }
    
    public int getPreferredHeight(int width)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        return theme.labelFont.getHeight() * tab.length + 2;
    }

    public void print(Graphics g, boolean selected, int width, int height)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        g.setColor(theme.fontColor);
        g.setFont(theme.labelFont);
        int left = 2, flagi = 0, lineHeight = theme.labelFont.getHeight();
        switch(alignment)
        {
            case ALIGN_RIGHT:
                left = width - 2;
                flagi = Graphics.TOP | Graphics.RIGHT;
                break;
            case ALIGN_CENTER:
                left = width / 2;
                flagi = Graphics.TOP | Graphics.HCENTER;
                break;
        }
        for(int i = 0; i < tab.length; ++i)
            g.drawString(tab[i], left, 1 + i * lineHeight, flagi);
    }

    
}
